package demo.controller;

import java.time.LocalDateTime;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class IndexController {
	
	
	public IndexController(){
		System.out.println(this.getClass().getSimpleName()+" run "+LocalDateTime.now());
	}
	
	
	@RequestMapping({"/index"})
	public ModelAndView index(HttpServletRequest request){
		String error = request.getParameter("error");
		if(error!=null){
			throw new RuntimeException(error);
		}
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("index");
		mv.addObject("now", new Date()+"");
		
		return mv;
	}
	

	
	@RequestMapping({"/forward2index"})
	public String forward2index(HttpServletRequest request){	
		return "forward:/";
	}
	
	@RequestMapping({"/redirect2myblog"})
	public String redirect2myblog(HttpServletRequest request){	
		return "redirect:http://codefun007.xyz/";
	}
	
	
	@RequestMapping({"/"})
	public ModelAndView home(HttpServletRequest request){
		String error = request.getParameter("error");
		if(error!=null){
			throw new RuntimeException(error);
		}
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("index");
		mv.addObject("now", new Date()+"");
		
		return mv;
	}
	

		
}